/**
 * Created by Administrator on 2018/5/4.
 */
define(function (require,exports,module) {
    require("../../lib/fullcalendar/moment.min");
    var VueComponent = Vue.extend({
        props:['parentData'],
        data:function () {
            return {
                messageFilter:{}
            };
        },
        mounted:function () {
            var $this = this;
            if(!Vue.prototype.messageType){
                Vue.prototype.messageType={};
            }
            window.onbeforeunload=function(){

                for(var  key in Vue.prototype.messageType){
                    try{
                        Vue.prototype.messageType[key].close();
                    }catch (e){

                    }
                }
                return  "lulu"
            }
            if(!Vue.prototype.messageType[this.parentData.messageType]){
                var href ="ws://"+location.host+basepath+"/message/"+this.parentData.messageType
                var  ws=new WebSocket(href);
                Vue.prototype.messageType[this.parentData.messageType]=ws;
                ws.onopen = function () {
                    setInterval(function () {
                        try{
                            if(ws.readyState==ws.CLOSED||ws.readyState==ws.CLOSING){
                                ws=new WebSocket(ws.url);
                            }
                            ws.send("heartbeat @"+moment().format("YYYY-MM-DD HH:mm:ss"));
                        }catch (e){

                        }

                    },30000);
                };
                ws.onmessage = function (evt) {
                    messages.$emit($this.parentData.messageType,evt);
                    console.log(evt);
                };

                ws.onclose = function () {
                };
            }
            axios.get(basepath+"/api/userSetting/messageFilter").then(function (response) {
                $this.messageFilter = response.data;
                for(var i  in $this.messageFilter) {
                    var val = $this.messageFilter[i];
                    for (var k in val) {
                        val[k] = k;
                    }
                }


            })



        },
        created:function (){

        },
        destoryed:function () {
           console.log("fuck you");
        }
    });
    module.exports = VueComponent;
    return VueComponent;

})